﻿//-----------------------------------------------------------------------
// <copyright file="AutoTruncatingBoldLabel.cs" company="Redmist">
//     Copyright (c) Damien T Smith. All rights reserved.
// </copyright>
// <author>Damien Smith</author>
//-----------------------------------------------------------------------

namespace Redmist.UsageMonitor.CustomControls
{
    using System.ComponentModel;
    using System.Windows.Forms;

    /// <summary>A custom label based on a Redmist.UsageMonitor.CustomControls.BoldLabel that automatically truncates the displayed text.</summary>
    public class AutoTruncatingBoldLabel : BoldLabel
    {
        /// <summary>Gets the full text originally set by the Text property of the label.</summary>
        public string FullText { get; private set; }

        /// <summary>Gets or sets the maximum text length before the displayed text will be truncated.</summary>
        [DefaultValue(16)]
        public int TruncationLength { get; set; }

        /// <summary>Gets or sets a System.Windows.Forms.ToolTip representing the ToolTip control to display the full text when it is longer than the truncation length.</summary>
        public ToolTip TruncationToolTip { get; set; }

        /// <summary>Gets or sets the text to be used for the label.</summary>
        /// <remarks>If the text is longer than the truncation length, it will be truncated and the full text can only be retrieved through the FullText property.</remarks>
        public override string Text
        {
            get
            {
                return base.Text;
            }

            set
            {
                this.FullText = value;
                this.SetupLabelText();
            }
        }

        /// <summary>Sets up the base text for the label and sets the correct value of the tool tip.</summary>
        private void SetupLabelText()
        {
            if (this.FullText == null || this.TruncationToolTip == null)
            {
                base.Text = this.FullText;
                return;
            }

            if (this.FullText.Length > this.TruncationLength)
            {
                this.TruncationToolTip.SetToolTip(this, this.FullText);
                base.Text = this.FullText.Substring(0, this.TruncationLength) + "...";
            }
            else
            {
                this.TruncationToolTip.SetToolTip(this, string.Empty);
                base.Text = this.FullText;
            }
        }
    }
}
